package com.ruoyi.web.controller.system;


import com.ruoyi.web.service.C2IntegrationTimeCostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/C2IntegrationTimeCost")
public class C2IntegrationTimeCostController {

    @Autowired
    private C2IntegrationTimeCostService c2IntegrationTimeCostService;

    @GetMapping("/byMonths")
    public ResponseEntity<Map<String, Object>> getMonthlyReleaseData() {
        try {
            // 调用 Service 层方法获取数据
            Map<String, Map<String, Integer>> monthlyData = c2IntegrationTimeCostService.getMonthlyReleaseData();

            // 构造前端可用的响应结构
            Map<String, Object> response = new HashMap<>();
            response.put("months", new ArrayList<>(monthlyData.keySet())); // 月份数据（X轴）

            // 提取不同的统计项
            List<Integer> releaseCounts = new ArrayList<>();
            List<Integer> totalCounts = new ArrayList<>();
            List<Integer> pubCounts = new ArrayList<>();

            for (Map.Entry<String, Map<String, Integer>> entry : monthlyData.entrySet()) {
                Map<String, Integer> counts = entry.getValue();
                releaseCounts.add(counts.getOrDefault("releaseCount", 0));
                totalCounts.add(counts.getOrDefault("totalCount", 0));
                pubCounts.add(counts.getOrDefault("pubCount", 0));
            }

            response.put("releaseCounts", releaseCounts); // releaseCount 数据
            response.put("totalCounts", totalCounts);     // totalCount 数据
            response.put("pubCounts", pubCounts);         // pubCount 数据

            return ResponseEntity.ok(response);

        } catch (Exception e) {
            e.printStackTrace();

            // 错误处理
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("error", "Failed to fetch data: " + e.getMessage());
            return ResponseEntity.internalServerError().body(errorResponse);
        }
    }

    @GetMapping("/byWeeks")
    public ResponseEntity<Map<String, Object>> getWeeklyReleaseData() {
        try {
            // 调用 Service 层方法获取按周的数据
            Map<String, Map<String, Integer>> weeklyData = c2IntegrationTimeCostService.getWeeklyReleaseData();

            // 构造前端可用的响应结构
            Map<String, Object> response = new HashMap<>();
            response.put("weeks", new ArrayList<>(weeklyData.keySet())); // 周数据（X轴）

            // 提取不同的统计项
            List<Integer> releaseCounts = new ArrayList<>();
            List<Integer> totalCounts = new ArrayList<>();
            List<Integer> pubCounts = new ArrayList<>();

            for (Map.Entry<String, Map<String, Integer>> entry : weeklyData.entrySet()) {
                Map<String, Integer> counts = entry.getValue();
                releaseCounts.add(counts.getOrDefault("releaseCount", 0));
                totalCounts.add(counts.getOrDefault("totalCount", 0));
                pubCounts.add(counts.getOrDefault("pubCount", 0));
            }

            response.put("releaseCounts", releaseCounts); // releaseCount 数据
            response.put("totalCounts", totalCounts);     // totalCount 数据
            response.put("pubCounts", pubCounts);         // pubCount 数据

            return ResponseEntity.ok(response);

        } catch (Exception e) {
            e.printStackTrace();

            // 错误处理
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("error", "Failed to fetch data: " + e.getMessage());
            return ResponseEntity.internalServerError().body(errorResponse);
        }
    }

    @GetMapping("/byYears")
    public ResponseEntity<Map<String, Object>> getYearlyReleaseData() {
        try {
            // 调用 Service 层方法获取按年的数据
            Map<String, Map<String, Integer>> yearlyData = c2IntegrationTimeCostService.getYearlyReleaseData();

            // 构造前端可用的响应结构
            Map<String, Object> response = new HashMap<>();
            response.put("years", new ArrayList<>(yearlyData.keySet())); // 年数据（X轴）

            // 提取不同的统计项
            List<Integer> releaseCounts = new ArrayList<>();
            List<Integer> totalCounts = new ArrayList<>();
            List<Integer> pubCounts = new ArrayList<>();

            for (Map.Entry<String, Map<String, Integer>> entry : yearlyData.entrySet()) {
                Map<String, Integer> counts = entry.getValue();
                releaseCounts.add(counts.getOrDefault("releaseCount", 0));
                totalCounts.add(counts.getOrDefault("totalCount", 0));
                pubCounts.add(counts.getOrDefault("pubCount", 0));
            }

            response.put("releaseCounts", releaseCounts); // releaseCount 数据
            response.put("totalCounts", totalCounts);     // totalCount 数据
            response.put("pubCounts", pubCounts);         // pubCount 数据

            return ResponseEntity.ok(response);

        } catch (Exception e) {
            e.printStackTrace();

            // 错误处理
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("error", "Failed to fetch data: " + e.getMessage());
            return ResponseEntity.internalServerError().body(errorResponse);
        }
    }


    @GetMapping("/byQuarters")
    public ResponseEntity<Map<String, Object>> getQuarterlyReleaseData() {
        try {
            // 调用 Service 层方法获取按季度的数据
            Map<String, Map<String, Integer>> quarterlyData = c2IntegrationTimeCostService.getDataByQuarter();

            // 构造前端可用的响应结构
            Map<String, Object> response = new HashMap<>();
            response.put("quarters", new ArrayList<>(quarterlyData.keySet())); // 季度数据（X轴）

            // 提取不同的统计项
            List<Integer> releaseCounts = new ArrayList<>();
            List<Integer> totalCounts = new ArrayList<>();
            List<Integer> pubCounts = new ArrayList<>();

            for (Map.Entry<String, Map<String, Integer>> entry : quarterlyData.entrySet()) {
                Map<String, Integer> counts = entry.getValue();
                releaseCounts.add(counts.getOrDefault("releaseCount", 0));
                totalCounts.add(counts.getOrDefault("totalCount", 0));
                pubCounts.add(counts.getOrDefault("pubCount", 0));
            }

            response.put("releaseCounts", releaseCounts); // releaseCount 数据
            response.put("totalCounts", totalCounts);     // totalCount 数据
            response.put("pubCounts", pubCounts);         // pubCount 数据

            return ResponseEntity.ok(response);

        } catch (Exception e) {
            e.printStackTrace();

            // 错误处理
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("error", "Failed to fetch data: " + e.getMessage());
            return ResponseEntity.internalServerError().body(errorResponse);
        }
    }
}
